using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TuyennvLibrary.ServerApi.Sms;
using System.Text.RegularExpressions;
using TuyennvLibrary.SqlClient;
namespace TuyennvLibrary.ServerApi.Models
{
    public class MOLogModel:BaseModel<MsgObject>
    {
        public long moId;
        public MOLogModel()
        {
            table = "MOLog";
        }
        public long  Insert(MsgObject obj)
        {
            try
            {
                long moId = 0;
                moId=long.Parse(DB.Insert(table, new SqlParameter[] {
					new SqlParameter("msisdn",obj.userid),
					new SqlParameter("command_code",obj.command_code),
					new SqlParameter("short_code",obj.serviceid),
					new SqlParameter("mobile_operator",obj.mobile_operator),
					new SqlParameter("message",obj.message),
					new SqlParameter("msg_type",obj.msg_type),
					new SqlParameter("request_id",obj.request_id),
					new SqlParameter("request_time",obj.request_time),
                    
                },true).ToString());
                this.moId = moId;
                return moId;
                
            
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public bool Update(MsgObject obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter("msisdn",obj.userid),
					new SqlParameter("command_code",obj.command_code),
					new SqlParameter("short_code",obj.serviceid),
					new SqlParameter("mobile_operator",obj.mobile_operator),
					new SqlParameter("message",obj.message),
					new SqlParameter("msg_type",obj.msg_type),
					new SqlParameter("request_id",obj.request_id),
					new SqlParameter("request_time",obj.request_time),

                },new SqlParameter[]{
					new SqlParameter("moId",obj.moId),
																																								 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Delete(long moId)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter("moId",moId),
																																								 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string[] ListID)
        {
            try
            {
                if (ListID != null)
                {
                    foreach (string ID in ListID)
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
		public bool DeleteMulti(string ListID)
        {
            try
            {
				if(ListID!=string.Empty)
				{
					foreach(string ID in ListID.Split(','))
					{
						Delete(int.Parse(ID));
					}
				}
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<MsgObject> GetByPage(int page, int pagesize, List<MsgObject> source)
        {
            List<MsgObject> list = new List<MsgObject>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<MsgObject> GetListBy(DataTable dt)
		{
            List<MsgObject> list = new List<MsgObject>();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        MsgObject obj = new MsgObject();
						obj.moId= (dr["moId"] is DBNull) ?0 : long.Parse(dr["moId"].ToString());
						obj.userid= (dr["msisdn"] is DBNull) ?string.Empty : dr["msisdn"].ToString();
						obj.command_code= (dr["command_code"] is DBNull) ?string.Empty : dr["command_code"].ToString();
						obj.serviceid= (dr["short_code"] is DBNull) ?string.Empty : dr["short_code"].ToString();
						obj.mobile_operator= (dr["mobile_operator"] is DBNull) ?string.Empty : dr["mobile_operator"].ToString();
						obj.message= (dr["message"] is DBNull) ?string.Empty : dr["message"].ToString();
						obj.msg_type= (dr["msg_type"] is DBNull) ?0 : int.Parse(dr["msg_type"].ToString());
						obj.request_id= (dr["request_id"] is DBNull) ?string.Empty : dr["request_id"].ToString();
						obj.request_time= (dr["request_time"] is DBNull) ?DateTime.Now : DateTime.Parse(dr["request_time"].ToString());
         
                        list.Add(obj);
                    }
                }
                return list;
		}
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);                
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public MsgObject GetBy(DataTable dt)
		{
			if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        MsgObject obj = new MsgObject();
						obj.moId= (dr["moId"] is DBNull) ?0 : long.Parse(dr["moId"].ToString());
						obj.userid= (dr["msisdn"] is DBNull) ?string.Empty : dr["msisdn"].ToString();
						obj.command_code= (dr["command_code"] is DBNull) ?string.Empty : dr["command_code"].ToString();
						obj.serviceid= (dr["short_code"] is DBNull) ?string.Empty : dr["short_code"].ToString();
						obj.mobile_operator= (dr["mobile_operator"] is DBNull) ?string.Empty : dr["mobile_operator"].ToString();
						obj.message= (dr["message"] is DBNull) ?string.Empty : dr["message"].ToString();
						obj.msg_type= (dr["msg_type"] is DBNull) ?0 : int.Parse(dr["msg_type"].ToString());
						obj.request_id= (dr["request_id"] is DBNull) ?string.Empty : dr["request_id"].ToString();
						obj.request_time= (dr["request_time"] is DBNull) ?DateTime.Now : DateTime.Parse(dr["request_time"].ToString());

                        return obj;
                    }
                }
            return new MsgObject();
		}
        public DataTable Search(string arrayMessage, string command_code,string phonenumber,string typeshortcode,string shortcode, string fromday, string target_day)
        {
            string query = "select * from " + table + " where 1=1";
            if (!string.IsNullOrEmpty(arrayMessage))
            {
                string[] arr = arrayMessage.Split('|');
                string where = "";
                foreach (string message in arr)
                {
                    where += string.Format(" or message like '{0}%'", message);
                }
                where = Regex.Replace(where, "^ or ", "");
                query += string.Format(" and ({0})", where);
            }
            if (!string.IsNullOrEmpty(command_code))
            {
                query += string.Format(" and command_code like '%{0}%'", command_code);
            }
            if (!string.IsNullOrEmpty(phonenumber))
            {
                query += string.Format(" and msisdn like '%{0}%'", phonenumber);
            }
            if (!string.IsNullOrEmpty(typeshortcode))
            {
                query += string.Format(" and short_code like '{0}'", typeshortcode.Replace("x", "%"));
            }
            if (!string.IsNullOrEmpty(shortcode) && !shortcode.Equals("0"))
            {
                query += string.Format(" and short_code='{0}'", shortcode);
            }
            if (!string.IsNullOrEmpty(fromday) && !string.IsNullOrEmpty(target_day))
            {
                query += string.Format(" and (request_time>=convert(datetime,'{0}',103) and request_time<=convert(datetime,'{1} 23:59:59',103))", fromday, target_day);

            }
            this.Count = GetCountBy(query);
            if (this.page != 0)
            {
                query = GetQueryPagging(query, "a.request_time desc", this.page, this.pagesize);
            }
            return DB.ExecuteDataTable(query, null);
        }
        public MsgObject GetByID(long moId)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("moId",moId),
																																								
                },null);
                return GetBy(dt);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

    }
}
